/**
 * Created by Flower付成华 on 2018/5/15.
 */

var rootpath = "";//所有文件依赖的根路径，通过客户端传递过来
var playstate = 0;//默认的播放类型， 0：单句，1：本课  2：复读
var audioHandler ;

var curpindex = 0;
var curpagenum = 0;
var curobject;
var ispause = 0;//是否暂停
var curtime;//当前的 settimeout
var istranslation = false;


var select_id;
var select_duration ;
var select_translation ;
var select_text ;
var select_sound ;


var strs = [];
var dicdata = {
    'classroom':'教室',
    'window':'窗户',
    'blackboard':'黑板',
    'light':'电灯',
    'picture':'图画',
    'door':'门',
    'teacher’s desk':'讲台',
    'computer':'计算机',
    'clean':'打扫'
};

var position_data ;//元数据



//判断手机类型
window.onload = function () {

    // 跳转本地页面、前面要加上local关键字

    //初始化audio
    initAudio();

    //执行默认
    ispause = 1;
    audioHandler.pause();
    $("#bottom_menu_img2").attr("src","../img/icon_play.png");


    $("#div_container div").css('border','0px solid white');
   
}

//初始化page
function initPage(){
//banner必备
        var swiper = new Swiper('.swiper-container', {
          spaceBetween:30,
          followFinger:false,
          touchAngle:90,
          speed:400,
            on: {
              // slideNextTransitionStart: function () {//下一页
              //   var index = swiper.activeIndex;
              //   console.log("slideNextTransitionStart"+index);
              //   // alert(index);
              // },
              // slidePrevTransitionStart:function (){// 上一页
              //   var index = swiper.activeIndex;
              //   console.log("slidePrevTransitionStart"+index);
              //   // alert(index);
              // },
              touchstart:function(){},
              touchmove:function(){},
              slideChangeTransitionStart:function(){//先执行start
                var index = swiper.activeIndex;
                //执行默认
                ispause = 1;
                audioHandler.pause();
                $("#bottom_menu_img2").attr("src","../img/icon_play.png");


                $("#div_container div").css('border','0px solid white');
                                
                //动态创建btn
                createCheckImage(index+1);
                
              }
            }
    });
}


//目录菜单的选择事件，然后跳转
function rightmenu_click(onthis){
    var topage = $$(onthis).attr("curpage");
    dismissRightMenu();
    if(isNotNull(swiper)){
        var mySwiper = document.querySelector('.swiper-container').swiper;
        mySwiper.slideTo(topage,1000);
    }
    
}


function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "";
    var seperator2 = "";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + "" + date.getHours() + seperator2 + date.getMinutes()
        + seperator2 + date.getSeconds();
    return currentdate;
}


function createCheckImage(pindex){
    console.log(pindex);
    if(position_data.pages.length>0){
        var contents =  position_data.contents;
        // for(var i = 1;i<= position_data.pages.length;i++){
            var pages = contents[pindex+''];
                $("#div_container").html("");
                if(isNotNull(pages) && pages.length>0){
                    var imgContainer=document.getElementById("div_container");
                    // $("#div_container").html("");
                    for (var j = 0; j<pages.length; j++) {
                        var x = pages[j].x;
                        var y = pages[j].y;
                        var w = pages[j].w;
                        var h = pages[j].h;
                        var duration = pages[j].duration;
                        var translationtips = pages[j].translation;
                        var curtext = pages[j].text;
                        var cursound = pages[j].sound;
                                var div;
                                    imgContainer.style.height=100+"%";
                                    div = document.createElement("div");
                                    var aa = Number(pindex);
                                    var bb = Number(j)+1;
                                    
                                    div.id = aa+"_"+bb;
                                    div.setAttribute("duration",duration);
                                    div.setAttribute("translation",translationtips);
                                    div.setAttribute("text",curtext);
                                    div.setAttribute("sound",cursound);
                                    
                                    
                                    div.style.width = w+"%";
                                    div.style.height = h+"%";
                                    div.style.border = "0px solid #0099FF";
                                    div.style.borderRadius = 5+'px';
                                    // div.style.position="absolute";
                                    div.style.left=x+"%";
                                    div.style.zIndex = "100";
                                    div.style.position = "absolute";
                                    div.style.top=y+"%";
                                    // div.style.backgroundColor="green";
                                    imgContainer.appendChild(div);
                                
                                div.onclick=function(){
                                    if(playstate == 4){//查词功能，点击弹出内容弹窗，选择复制查词
                                        searchWords(this);
                                        return;
                                    }



                                    read(this,pindex,pages.length);

                                };

                    }

                    return;
                // }    
            
        }
    }
    

}



//初始化audio
function initAudio(){

    var curalltime = 5;
    audioHandler = new AudioPlayer({
       id: 'music_player'
    });

    audioHandler.on('canplay', function (totalTime, name, item) {
        audioHandler.play();
        console.log('canplay');
        curalltime = totalTime;
    });

    audioHandler.on('play' , function (){
        console.log('play');

        curtime = setTimeout(function(){

         if(playstate == 0){
             $("#bottom_menu_img2").attr("src","../img/icon_play.png");
             $("#div_tips").hide();

         }else if(playstate == 1){//全局阅读

             var select_ids = select_id.split("_");
             if(select_ids[1]>=curpagenum || ispause){
                audioHandler.pause();
                 $("#bottom_menu_img2").attr("src","../img/icon_play.png");
                 $("#div_tips").hide();

             }else{
                 read($("#"+select_id),curpindex,curpagenum);    
             }

         }else if(playstate == 2){//循环阅读

            if(ispause){
                
            }else{
                audioHandler.pause();
                read(curobject,curpindex,curpagenum);
            }
         }else if(playstate == 3){//连续

         }else if(playstate == 4){//查词
            

         }
          
        },curalltime*1200)
    });

}




//开始阅读
function read(ob,pindex,pagenum){
    
    //全局
    curpagenum = pagenum;
    curpindex = pindex;
    curobject = ob;

    //select 变量
    select_id = $(ob).attr("id")+"";
    select_sound = $(ob).attr("sound")+"";
    select_text = $(ob).attr("text")+"";
    select_translation = $(ob).attr("translation")+"";
    select_duration = $(ob).attr("duration")+"";

    $("#div_container div").css('border','0px solid white');
    $(ob).css('border','1px solid #0099FF');

    var msrc = rootpath+"/"+select_sound;
     $("#bottom_menu_img2").attr("src","../img/icon_unplay.png");
     $("#div_tips").hide();
     $("#div_tips").html(select_translation);
     if(istranslation){
        $("#div_tips").show();   
     }
     
     var list = [{
        name: select_text,
        url: msrc,
        time: select_duration
    }];

    ispause = 0;
    clearTimeout(curtime);
    audioHandler.clearList();
    audioHandler.add(list);
    audioHandler.loadIndex(0);
}


function showmenu(){
    $("#div_words_bg").show();
    toshare();
}


//点击改变播放音频的状态
function readstate(){

   /* if($("#bgmusic").get(0).paused){//暂停
        $("#bgmusic").get(0).play();
        
        $("#bottom_menu_img2").attr("src","../img/icon_unplay.png");
    }else{
        $("#bgmusic").get(0).pause();
        $("#bottom_menu_img2").attr("src","../img/icon_play.png");
    }*/

    if(audioHandler.playing){//暂停
        ispause = 1;
        audioHandler.pause();
        $("#bottom_menu_img2").attr("src","../img/icon_play.png");
    }else{
        ispause = 0;
        audioHandler.play();
        $("#bottom_menu_img2").attr("src","../img/icon_unplay.png");
        
    }
}


//弹出层
function toshare(){
        $(".am-share").addClass("am-modal-active"); 
        if($(".sharebg").length>0){
            $(".sharebg").addClass("sharebg-active");
        }else{
            $("body").append('<div class="sharebg"></div>');
            $(".sharebg").addClass("sharebg-active");
        }
        $(".sharebg-active,.share_btn").click(function(){
            $(".am-share").removeClass("am-modal-active");  
            setTimeout(function(){
                $(".sharebg-active").removeClass("sharebg-active"); 
                $(".sharebg").remove(); 
            },300);
        })
} 

function menu1(){
    $("#div_words_bg").hide();
    $("#li_menu4").css("background-image","url(../img/icon_menu1_off.png)");
    $("#li_menu5").css("background-image","url(../img/icon_menu2_off.png)");
    $("#li_menu6").css("background-image","url(../img/icon_menu3_off.png)");
    $("#li_menu7").css("background-image","url(../img/icon_menu4_off.png)");
    $("#li_menu8").css("background-image","url(../img/icon_menu5_off.png)");
    
    $("#li_menu4").css("background-image","url(../img/icon_menu1_on.png)");
    $(".am-share").removeClass("am-modal-active");  
            setTimeout(function(){
                $(".sharebg-active").removeClass("sharebg-active"); 
                $(".sharebg").remove(); 
            },300);
    playstate = 0;
}  

function menu2(){
    $("#div_words_bg").hide();
    $("#li_menu4").css("background-image","url(../img/icon_menu1_off.png)");
    $("#li_menu5").css("background-image","url(../img/icon_menu2_off.png)");
    $("#li_menu6").css("background-image","url(../img/icon_menu3_off.png)");
    $("#li_menu7").css("background-image","url(../img/icon_menu4_off.png)");
    $("#li_menu8").css("background-image","url(../img/icon_menu5_off.png)");


    $("#li_menu5").css("background-image","url(../img/icon_menu2_on.png)");
    $(".am-share").removeClass("am-modal-active");  
            setTimeout(function(){
                $(".sharebg-active").removeClass("sharebg-active"); 
                $(".sharebg").remove(); 
            },300);

            playstate = 1;
} 

 function menu3(){
    $("#div_words_bg").hide();
    $("#li_menu4").css("background-image","url(../img/icon_menu1_off.png)");
    $("#li_menu5").css("background-image","url(../img/icon_menu2_off.png)");
    $("#li_menu6").css("background-image","url(../img/icon_menu3_off.png)");
    $("#li_menu7").css("background-image","url(../img/icon_menu4_off.png)");
    $("#li_menu8").css("background-image","url(../img/icon_menu5_off.png)");


    $("#li_menu6").css("background-image","url(../img/icon_menu3_on.png)");
    $(".am-share").removeClass("am-modal-active");  
            setTimeout(function(){
                $(".sharebg-active").removeClass("sharebg-active"); 
                $(".sharebg").remove(); 
            },300);

            playstate = 2;
} 


function menu4(){
    $("#div_words_bg").hide();
    $("#li_menu4").css("background-image","url(../img/icon_menu1_off.png)");
    $("#li_menu5").css("background-image","url(../img/icon_menu2_off.png)");
    $("#li_menu6").css("background-image","url(../img/icon_menu3_off.png)");
    $("#li_menu7").css("background-image","url(../img/icon_menu4_off.png)");
    $("#li_menu8").css("background-image","url(../img/icon_menu5_off.png)");


    $("#li_menu7").css("background-image","url(../img/icon_menu4_on.png)");
    $(".am-share").removeClass("am-modal-active");  
            setTimeout(function(){
                $(".sharebg-active").removeClass("sharebg-active"); 
                $(".sharebg").remove(); 
            },300);

            playstate = 1;
}

function menu5(){
    $("#div_words_bg").hide();
    $("#li_menu4").css("background-image","url(../img/icon_menu1_off.png)");
    $("#li_menu5").css("background-image","url(../img/icon_menu2_off.png)");
    $("#li_menu6").css("background-image","url(../img/icon_menu3_off.png)");
    $("#li_menu7").css("background-image","url(../img/icon_menu4_off.png)");
    $("#li_menu8").css("background-image","url(../img/icon_menu5_off.png)");


    $("#li_menu8").css("background-image","url(../img/icon_menu5_on.png)");

    $(".am-share").removeClass("am-modal-active");  
            setTimeout(function(){
                $(".sharebg-active").removeClass("sharebg-active"); 
                $(".sharebg").remove(); 
            },300);

            playstate = 4;
}


//开始点击查词,弹出框框
function searchWords(ob){

    select_sound = $(ob).attr("sound")+"";
    select_text = $(ob).attr("text")+"";
    select_translation = $(ob).attr("translation")+"";
    select_duration = $(ob).attr("duration")+"";


    $("#div_words_dialog_content").html(select_text);
    $("#div_words_dialog").show();
    $("#div_words_bg").show();
}

//关闭弹窗
function closeDialog(){
    $("#div_words_dialog").hide();
    $("#div_words_bg").hide();
}

//显示目录菜单
function showRightMenu(){

    $("#div_right").show();
    $("#div_words_bg").show();
}

//关闭目录菜单
function dismissRightMenu(){

    $("#div_right").hide();
    $("#div_words_bg").hide();   
}


//获取选中的文本，执行查词功能
function getCurText(){

    var text = window.getSelection().toString();

    if((Object.keys(dicdata)).indexOf(text) == -1){
        //开始弹出查词的框
        alert("查词解释：  暂无此解释");
    }else{
        //开始弹出查词的框
        alert("查词解释：  "+text+": "+dicdata[text]);
    }
    

}

// http://dict-co.iciba.com/api/dictionary.php?w=go&key=DE5EB7C205E95AB2884CB14102E64369&type=json
//是否需要翻译提示
function translation_change(){
    if(istranslation){
        $("#bottom_menu_img3").attr('src', '../img/img_translation_off.png'); 
        istranslation = false;
    }else{
        $("#bottom_menu_img3").attr('src', '../img/img_translation_on.png'); 
        istranslation = true;
    }
}
